package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;

    //3. 添加角色
    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
    //1. 角色列表查询
    @Override
    public  PageResult<Role> list(String name,String label, Integer page, Integer pageSize){
        //1.设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接方法，执行查询
        List<Role> RoleList = roleMapper.selectList(name,label);
        //3.解析并封装成PageResult返回分页结果
        Page<Role> R = (Page<Role>) RoleList;
        return new PageResult<>(R.getTotal(), R.getResult());
    }
    //2. 删除角色
    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }
    //4. 根据ID查询
    @Override
    public Role getById(Integer id) {
        return roleMapper.selectById(id);
    }
    //5. 修改角色
    @Override
    public void update(Role role) {
        //1.补全基础属性
        role.setUpdateTime(LocalDateTime.now());
        //2.调用mapper
        roleMapper.update(role);
    }
    //6. 查询所有角色
    @Override
    public List<Role> getAllRoles() {
        return roleMapper.getAllRoles();
    }
}
